let timeId = null;
import {
    url
} from '../../modelApi/url_config.js'
import {
    getStrLength
} from '../../utils/util.js'
var App = getApp();
Page({
    data: {
        shopType: 0, // 版本，游客，会员版本: 0, 店家版本 1;
        balance: '',
        validBalance: '',
        logined: null, //登陆判断
        isMember: null, // 会员 判断
        anchorNum: 0,
        agentNum: 0,
        isStore: null,
        grade: '',
        gradeTitle: '',
        isSendPower: false, // 是否显示赠送权限
        sendUserId: '', //赠送人id
        rightType: '', // 权限类型
        rightTypeName: '', // 权限名字
        getStrLengthFlag: false,
    },
    onLoad(options) {
        if (!App.globalData.isShow) {
            console.log(!App.globalData.isShow)
            wx.redirectTo({
                url: "/pages/errorPage/index"
            });
            return
        }
        if (JSON.stringify(options) != "{}") {
            this.setData({
                rightType: options.rightType,
                rightTypeName: Number(options.rightType) === 2 ? '经纪人权益' : '主播权益',
                sendUserId: options.sendUserId,
                sendNickName: options.sendNickName,
                isSendPower: options.rightType ? true : false
            })
            if (this.data.isSendPower) {
                wx.hideTabBar();
            }
        }
        if (!wx.getStorageSync("tel")) {
            if (JSON.stringify(options) != "{}") {
                let UrlData = `/pages/mine/index?rightType=${options.rightType}&sendUserId=${options.sendUserId}&sendNickName=${options.sendNickName}`;
                wx.setStorageSync("backPage", UrlData);
            }
            wx.redirectTo({
                url: "/pages/register/index"
            });
        }
        if (wx.getStorageSync('nickName')) {
            this.setData({
                getStrLengthFlag: getStrLength(wx.getStorageSync('nickName')) >= 14
            })
        }
    },
    onShow() {
        this.balanceFunc();
        this.getRightNumsFunc();
        this.memberInfoFunc(); // 获取会员信息接口
    },
    /*切换*/
    changeName() {
        var that = this;
        if (!that.data.shopType) {
            wx.hideTabBar();
            that.setData({
                shopType: 1
            })
        } else {
            wx.showTabBar();
            that.setData({
                shopType: 0
            })
        }
    },
	onShareAppMessage(res) {
        let typeName = Number(res.target.dataset.index) === 2 ? '经纪人权益' : '主播权益';
        let url_data = `pages/mine/index?rightType=${res.target.dataset.index}&sendUserId=${wx.getStorageSync('userId')}&sendNickName=${wx.getStorageSync('nickName')}`;
		return {
			title: wx.getStorageSync('nickName') + '赠送你一个' + typeName,
			path: url_data,
			imageUrl: 'https://lundao01.oss-cn-beijing.aliyuncs.com/wxapp/static/img/global_logo.png'
		}
	},
    todetial(e) {
        wx.navigateTo({
          url: `/pages/busSchoollist/index?type=${e.currentTarget.dataset.type}`,
        })
    },
    tocoup() {
        wx.navigateTo({
            url: '/pages/mine/sonpages/couponList/index'
        })
    },
    toGrade() {
        wx.navigateTo({
            url: '/pages/upgradeList/index?type=grade'
        })
    },
    /* 跳转提现 */
    toTixianFun() {
        return
        wx.navigateTo({
            url: '/pages/tixian/index'
        })
    },
    memberInfoFunc() {
        App.ajax(url.memberInfo, {
            userId: wx.getStorageSync('userId')
        }, 'get').then(res => {
            if (+res.code === 0) {
                this.setData({
                    grade: res.data.grade,
                    gradeTitle: res.data.gradeTitle,
                    isMember: res.data.isMember,
                    isStore: res.data.isStore
                })
                wx.setStorageSync('grade', res.data.grade)
                wx.setStorageSync('isStore', res.data.isStore);
                wx.setStorageSync('isMember', res.data.isMember);
                wx.setStorageSync('isAnchor', res.data.isAnchor);
            }
        })
    },
    getRightNumsFunc() {
        App.ajax(url.getRightNums, {}, 'get').then(res => {
            if (+res.code === 0) {
                this.setData({
                    anchorNum: res.data.anchorNum,
                    agentNum: res.data.agentNum
                })
            }
        })
    },
    balanceFunc() {
        App.ajax(url.balance, {}, 'get').then(res => {
            if (+res.code === 0) {
                this.setData({
                    balance: res.data ? res.data.balance : 0,
                    validBalance:  res.data ? res.data.validBalance : 0
                })
            }
        })
    },
    /* 跳转订单页面 */
    goOrderList(event) {
        wx.navigateTo({
            url: "/pages/mine/sonpages/order/orderList/index?orderstatus=" + event.currentTarget.dataset.gid
        });
    },
    goCouponList: function () {
        wx.navigateTo({
            url: "/pages/mine/sonpages/couponList/index"
        });
    },
    /* 跳转创建直播间页面 */
    goLivePush: function () {
        wx.navigateTo({
            url: "/pages/buildliveroom/index"
        });
    },
    /* 跳转升级 */
    goUpgrade: function () {
        wx.navigateTo({
            url: "/pages/upgradeList/index"
        });
    },
    addManaFunc() {
        wx.navigateTo({
            url: '/pages/address/index',
        })
    },
    toScholl() {
        wx.navigateTo({
            url: '/pages/busSchool/index',
        })
    },
    toFans() {
        wx.navigateTo({
            url: '/pages/fansList/index',
        })
    },
    /* 获取赠送的权限 */
    getSendType() {
        wx.showTabBar();
        let params = {
            rightType: this.data.rightType,
            userId: this.data.sendUserId
        }
        App.ajax(url.receiveRightUrl, params, 'post').then(res => {
            this.setData({
                isSendPower: false
            });
            if (+res.code === 0) {
                this.getRightNumsFunc();
                this.memberInfoFunc(); // 获取会员信息接口
                wx.showToast({
                    title: '领取成功',
                    icon: 'success'
                })
            } else {
                wx.showToast({
                    title: '领取失效',
                    icon: 'none'
                })
            }
        })
    }
})